package com.st.service.impl;

import com.api.userinfo.UserInfoClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.*;
import com.st.mapper.IUserMapper;
import com.st.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class UserServiceImpl extends ServiceImpl<IUserMapper, User> implements IUserService {
    @Autowired
    private IUserMapper iUserMapper;

    @Autowired
    private UserInfoClient userInfoClient;

    @Override
    public PageResult<User> findAll(PageDTO<User> pageDTO) {
        //数据校验
        if (pageDTO == null) {
            return new PageResult<User>(false, "参数异常");
        }
        User user = pageDTO.getData();
        //分页
        IPage<User> iPage = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        IPage<User> userPage = iUserMapper.findUser(iPage,user);
        //返回信息
        PageResult pageResult = new PageResult();
        pageResult.setData(userPage.getRecords());
        pageResult.setTotal(userPage.getTotal());
        pageResult.setSuccess(true);
        pageResult.setMsg("查询成功");
        return pageResult;
    }

    @Override
    public Result delUser(String uid) {
        if (StringUtils.isEmpty(uid)){
            return new Result(false,"用户不存在");
        }
        int i = iUserMapper.deleteById(uid);
        return new Result(i==1?true:false,i==1?"删除成功":"删除失败");
    }

    @Override
    public Result updateUser(User user) {
        if (user == null) {
            return new Result(false,"用户不存在");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(user.getUserType())) {
            queryWrapper.eq("user_id",user.getUserId());
            int i = iUserMapper.update(user, queryWrapper);
            return new Result(i==1?true:false,i==1?"设置成功":"设置失败");
        }
//        if (!StringUtils.isEmpty(user.getUserStatus())) {
//            queryWrapper.eq("user_status",user.getUserStatus());
//            int i = iUserMapper.update(user, queryWrapper);
//            return new Result(i==1?true:false,i==1?"设置成功":"设置失败");
//        }
        return new Result(false,"数据错误");
    }

    @Override
    public Result searchUserInfo(UserInfo userInfo) {
        if(userInfo.getUserInfoId()==null){
            return new Result(false,"参数异常");
        }
        Integer id = userInfo.getUserInfoId();
        UserInfo user = userInfoClient.searchUserInfo(id);
        return new Result(true,"查询成功",user);
    }
}
